package com.huatai.exchange.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.faneui.common.utils.ListUtils;
import com.huatai.exchange.dal.dao.ExcFileLoadRecordDAO;
import com.huatai.exchange.dal.model.ExcFileLoadRecord;
import com.huatai.exchange.facade.enums.LoadRecordStatusEnum;
import com.huatai.exchange.service.ExcFileLoadRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ExcFileLoadRecordServiceImpl implements ExcFileLoadRecordService {

    @Autowired
    private ExcFileLoadRecordDAO excFileLoadRecordDAO;

    @Override
    public void addExcFileLoadRecord(String loadId,String occurDate) throws BusinessException {
        ExcFileLoadRecord record = new ExcFileLoadRecord();
        record.setLoadId(loadId);
        record.setOccurDate(occurDate);
        addExcFileLoadRecord(record);
    }

    @Override
    public void addExcFileLoadRecord(ExcFileLoadRecord excFileLoadRecord) throws BusinessException {
        try {
            if(StringUtils.isBlank(excFileLoadRecord.getOccurDate()))
                excFileLoadRecord.setOccurDate(DateFormatUtils.yyyyMMdd.format(new Date()));
            excFileLoadRecord.setOccurTime(DateFormatUtils.HHmmss.format(new Date()));
            excFileLoadRecord.setRequestTimes(0);
            excFileLoadRecord.setStatus("I");
            excFileLoadRecordDAO.insert(excFileLoadRecord);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

    @Override
    public ExcFileLoadRecord queryExcFileLoadRecord(String id) throws BusinessException {
        ExcFileLoadRecord record = null;
        try {
            record = excFileLoadRecordDAO.queryById(id);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return record;
    }

    @Override
    public List<ExcFileLoadRecord> queryExcFileLoadRecordList(ExcFileLoadRecord excFileLoadRecord) throws BusinessException {
        List<ExcFileLoadRecord> records = null;
        try {
            records = excFileLoadRecordDAO.queryByModel(excFileLoadRecord);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return records;
    }

    @Override
    public ExcFileLoadRecord queryExcFileLoadRecord(String loadId, String occurDate) throws BusinessException {
        ExcFileLoadRecord param = new ExcFileLoadRecord();
        param.setLoadId(loadId);
        param.setOccurDate(occurDate);
        List<ExcFileLoadRecord> records = queryExcFileLoadRecordList(param);
        if (ListUtils.isEmpty(records))
            return null;
        // 查询完 需要更新 查询次数
        ExcFileLoadRecord record = records.get(0);
        return record;
    }

    @Override
    public String queryExcFileLoadPath(String thirdFileId, String occurDate) throws BusinessException {
        ExcFileLoadRecord record = queryExcFileLoadRecord(thirdFileId, occurDate);
        if (record == null ) {
            return null;
        }
        if(!LoadRecordStatusEnum.S.toString().equals(record.getStatus())) {
            return null;
        }
        try{
            excFileLoadRecordDAO.updateRequestTimes(record.getId());
        }catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return record.getFilePath();
    }

    @Override
    public void modifyExcFileLoadRecord(ExcFileLoadRecord excFileLoadRecord) throws BusinessException {
        try {
            Long num = excFileLoadRecordDAO.updateById(excFileLoadRecord);
            if (num == 0) {
                throw new BusinessException("更新失败");
            }
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }

    @Override
    public Long modifyRecordStatus(String id, String oldStatus, String newStatus,String memo) throws BusinessException {
        Long num = null ;
        try {
            num  = excFileLoadRecordDAO.updateStatus(id, oldStatus, newStatus,memo);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
        return num;
    }
}
